package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.AssessHistory;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.utility.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class SellCarHistoryAdapter extends BaseAdapter {

    private List<AssessHistory> mAssessHistories;

    private Context mContext;

    private CategoryData mCategoryData;

    private LayoutInflater mLayoutInflater;

    private boolean isEdit = false;

    public SellCarHistoryAdapter(List<AssessHistory> assessHistories, Context context) {
        mAssessHistories = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mCategoryData = GPJApplication.getInstance().getCategoryData();
    }

    public void setData(List<AssessHistory> mAssessHistories) {
        this.mAssessHistories = mAssessHistories;
        notifyDataSetChanged();
    }


    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();

    }


    @Override
    public int getCount() {
        return mAssessHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return mAssessHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.sellcar_his_item, null);
            holder = new ViewHolder();
            holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.car_img);
            holder.carName = (TextView) convertView.findViewById(R.id.carName);
            holder.plat_count = (TextView) convertView.findViewById(R.id.plat_count);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.car_status = (TextView) convertView.findViewById(R.id.car_status);
            holder.car_plat = (TextView) convertView.findViewById(R.id.car_plat);
            holder.mainView = convertView.findViewById(R.id.main);
            holder.paltMain = (LinearLayout) convertView.findViewById(R.id.platmain);
            holder.moreTextView = (TextView) convertView.findViewById(R.id.more);
            holder.switchImageView = (ImageView) convertView.findViewById(R.id.switch_id);
            holder.redImageView = (ImageView) convertView.findViewById(R.id.red);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        final AssessHistory source = mAssessHistories.get(position);
        convertView.setTag(R.id.tag_second, source);

        if (isEdit) {
            holder.switchImageView.setVisibility(View.VISIBLE);
        } else {
            holder.switchImageView.setVisibility(View.GONE);
            source.isDelete = false;
        }
        if (source.isDelete) {
            holder.switchImageView.setImageResource(R.drawable.checked);
        } else {
            holder.switchImageView.setImageResource(R.drawable.uncheck);
        }

        holder.carName.setText(source.title);
        holder.car_plat.setText(source.sell_type);

        String plat_counts = source.plat_counts;
        holder.plat_count.setText(Utils.changeColor(mContext, "已为您向 " + plat_counts + " 个平台提交", 5, 5 + plat_counts.length(), R.color.new_color));
        holder.time.setText(source.create_time);
        holder.car_status.setText(Utils.changeColor(mContext, "车辆状态: " + source.car_status, 6, 6 + source.car_status.length(), R.color.status_color));
        String filename = mCategoryData.getLogoByModer(source.model_slug);
        String logo_url =
                Const.IMG_DOMAIN
                        + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                        + filename + "?imageView2/0/w/100/h/100";

        ImageLoad.LoadImage(holder.imgThumbnail, logo_url, R.drawable.car_loading, R.drawable.car_no,mContext);
        holder.paltMain.removeAllViews();

        if (source.plat_list.size() <= 1) {
            holder.mainView.setVisibility(View.GONE);
        } else {
            holder.mainView.setVisibility(View.VISIBLE);
            int size = source.plat_list.size();
            if (size > 4) {
                size = 4;
            }
            for (int i = 0; i < size; i++) {
                View view = mLayoutInflater.inflate(R.layout.plat_item, null);
                TextView text1 = (TextView) view.findViewById(R.id.text1);
                TextView text2 = (TextView) view.findViewById(R.id.text2);
                TextView text3 = (TextView) view.findViewById(R.id.text3);
                text1.setText(source.plat_list.get(i).getUpdate_time());
                text2.setText(source.plat_list.get(i).getPlat_name());
                text3.setText(source.plat_list.get(i).getStatus_str());
                holder.paltMain.addView(view);
            }
        }

        if (source.platsJSONArray.length() > 0 ) {
            holder.redImageView.setVisibility(View.VISIBLE);
        } else {
            holder.redImageView.setVisibility(View.GONE);
        }


//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                holder.redImageView.setVisibility(View.GONE);
//                Intent intent = new Intent(mContext, SellCarDetailActivity.class);
//                intent.putExtra("typevalue", source.car_id);
//                mContext.startActivity(intent);
//            }
//        });

        return convertView;
    }

    public static class ViewHolder {
        public ImageView imgThumbnail;
        public TextView moreTextView;
        public LinearLayout paltMain;
        public View mainView;
        public TextView car_plat;
        public TextView car_status;
        public TextView time;
        public TextView plat_count;
        public TextView carName;
        public ImageView switchImageView;
        public ImageView redImageView;
    }

}
